/* global ICENTER_username ICENTER_password */

import service from './index';
import JsEncrypt from 'jsencrypt';

let ICENTER_URL = 'http://192.168.30.60:8310';
// -----------------------------------------------------------------------------//
// 获取token信息
export async function getToken(serviceName) {
    ICENTER_URL = serviceName ? serviceName : 'http://192.168.30.60:8310';
    let time = await service('get', ICENTER_URL + `/user/jwt/time`);
    let newPassword = getRsaPassWord(time.data + ICENTER_password);
    return service('post', ICENTER_URL + `/user/jwt/token`, {
        username: ICENTER_username,
        password: newPassword,
    });
}

function getRsaPassWord(password) {
    const jse = new JsEncrypt();
    jse.setPublicKey(`-----BEGIN PUBLIC KEY-----
 MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANbuVd2AW+xnDH82hNWFYjDWttKilGgt
 S+tVxBQF4POl7pkNccQNEfrklGsBvR1U4yjNhVeoLmO/Xd6WF0u0HsMCAwEAAQ==
 -----END PUBLIC KEY-----`);
    return jse.encrypt(password);
}

/**
 * 获取时序影像列表
 */
export function getAllTimeService() {
    return service('get', ICENTER_URL + '/tilecache/api/v1/layers', {
        page: 1,
        pageSize: 100,
        order: 'DESC',
        isBase: false,
        // layerName: 'NDVI031-PNG-4326',
        layerType: 'TIMELAYER',
    });
}
/**
 * 获取时序影像时间段
 * @param {*} params
 */
export function getTimeServiceParams(params) {
    return service('get', ICENTER_URL + '/tilecache/service/time', params);
}
